package org.dromara.batch;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

// 成交状态和币价判断任务
public class OrderStatusCheckTasklet implements Tasklet {
    private TradingService tradingService;
    private CoinPriceService coinPriceService;
    private double targetPrice;

    public OrderStatusCheckTasklet(TradingService tradingService, CoinPriceService coinPriceService, double targetPrice) {
        this.tradingService = tradingService;
        this.coinPriceService = coinPriceService;
        this.targetPrice = targetPrice;
    }

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        boolean isFilled = tradingService.isOrderFilled();
        double currentPrice = coinPriceService.getCurrentPrice();
        System.out.println("Order filled: " + isFilled + ", Current price: " + currentPrice + ", Target price: " + targetPrice);
        if (!isFilled || currentPrice <= targetPrice) {
            chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("shouldBuyProduct", true);
        } else {
            chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("shouldBuyProduct", false);
        }
        return RepeatStatus.FINISHED;
    }
}
